package com.example.gamecenter.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.gamecenter.R

class ExploreRecyclerAdapter(private val data: List<RecommendItem>):  RecyclerView.Adapter<ExploreRecyclerAdapter.ViewHolder>() {
    private lateinit var ctx :Context;

    interface OnDetailItemClickListener {
        /** 查看详情 */
        fun onDetail(v: View?, parmas: RecommendItem?=null);
    }
    private var onDetailClickListener: OnDetailItemClickListener? = null;
    fun setOnDetailClickListener(listener: OnDetailItemClickListener){
        onDetailClickListener = listener;
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        ctx = parent.context;
        val view = LayoutInflater.from(parent.context).inflate(R.layout.recommend_item, parent, false)

        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = data[position]
        holder.bind(item)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)  {
        private val textView : TextView = itemView.findViewById(R.id.recommend_item_title)
        private val imageView : ImageView = itemView.findViewById(R.id.recommend_item_image)
        private val recommend_item_view : Button = itemView.findViewById(R.id.recommend_item_view);
        fun bind(item: RecommendItem){
            textView.text = item.title;
            recommend_item_view.setOnClickListener {
                onDetailClickListener?.onDetail(itemView, item)
            }
            Glide.with(itemView)
                .load(item.imageUrl)
                .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(30)))
                .into(imageView);
        }
    }
}